package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IMagazineNameDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.StringUtils;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.homeSite.MagazineName;

public class MagazineNameDaoImpl extends HibernateDaoSupport implements IMagazineNameDao{

	public void add(MagazineName magazineName) throws DataAccessException {
		this.getHibernateTemplate().save(magazineName);
	}

	public void delete(MagazineName magazineName) throws DataAccessException {
		this.getHibernateTemplate().delete(magazineName);
	}

	public MagazineName loadById(Integer id) throws DataAccessException {
		Object obj = null;
		MagazineName magazineName = new MagazineName();
		obj =  this.getHibernateTemplate().get(MagazineName.class, id);
		if(obj!=null){
			magazineName = (MagazineName)obj;
		}
		return magazineName;
	}

	public void update(MagazineName magazineName) throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(magazineName);
	}
	
	@SuppressWarnings("unchecked")
	public List<MagazineName> loadAll() throws DataAccessException {
		String sql = " from MagazineName as m where 1 = 1 order by m.inputtime desc";
		return this.getHibernateTemplate().find(sql);
	}
	
	
	/* 通过期刊名,刊首语 , 是否推荐, 是否可读 开始时间,结束时间 */
	public PageResultSet loadPageList(String name,String jj,Short isCommend, Short iskd,String beginDate,String endDate,Integer page,Integer pageSize) throws DataAccessException {
		PageResultSet pageResultSet = new PageResultSet();
	    PerfectPageResultSet pageResult = new PerfectPageResultSet();
	    // 获取Session
	    Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);

	    // 初始化SQL语句
	    StringBuffer stringBuffer = new StringBuffer(
	            " from MagazineName as m  where 1=1 ");
	    
	    // 设置每页显示得记录数和开始页
	    pageResult.setPage(page, pageSize);

	    // 参数
	    List<Comparable> list = new ArrayList<Comparable>();
	    
	    // 期刊名
	    if(name!=null && !name.equals(""))
	    {
	    	name = "%"+name+"%";
	        stringBuffer.append(" and m.qiname like ?");
	        list.add(name);
	    }
	    
	    // 刊首语
	    if(jj!=null && !jj.equals(""))
	    {
	    	jj = "%"+jj+"%";
	        stringBuffer.append(" and m.qicontent like ?");
	        list.add(jj);
	    }
	    
	    // 是否推荐 0 否 1 是
	    if(isCommend!=null&&!isCommend.equals((short)-1))
	    {
	    	stringBuffer.append(" and m.isCommend = ?");
	        list.add(isCommend);
	    }
	    
	    // 是否是外连接 0 否 1 是
	    if(iskd!=null&&!iskd.equals((short)-1))
	    {
	    	stringBuffer.append(" and m.iskd = ?");
	        list.add(iskd);
	    }
	    
	    // 时间段
	    if(null!=beginDate && !"".equals(beginDate) && null!=endDate && !"".equals(endDate)){
	    	stringBuffer.append(" and m.inputtime >= '"+beginDate.trim()+" 00:00:00' and m.inputtime <= '"+endDate.trim()+" 23:59:59'");
	    }
	    
	    stringBuffer.append(" order by id desc ");

	    // 把集合转换成数组
	    String[] strParams = Tool.toArray(list);
	    
	    pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
	    
	    return pageResultSet;
	}

	public List loadMagazineNameByInputtime(String year) throws DataAccessException {
		
		List list=new ArrayList();
		int yea=Integer.valueOf(year)+1;
		if(year!=null&&!year.equals("-1")&&!year.equals("")){
			String sql="from MagazineName as m where m.inputtime >= ? and m.inputtime < ?";
			list=this.getHibernateTemplate().find(sql,new String[]{year,String.valueOf(yea)});
		}
		
		return list;
	}

	public MagazineName loadByName(String name) throws DataAccessException {
		String sql = " from MagazineName as m where m.qiname = ? ";
		MagazineName m  = null;
		List list = this.getHibernateTemplate().find(sql,name);
		if(list != null && list.size()>0){
			m = (MagazineName)list.get(0);
		}
		return m;
	}

	@SuppressWarnings("unchecked")
	public List<MagazineName> loadByTime(Integer top) throws DataAccessException {
		String sql = " from MagazineName as m order by m.inputtime desc ";
		List list = this.getSession().createQuery(sql).setMaxResults(top).list();
		if(StringUtils.isNotNull(list)){
			return list;
		}
		return null;
	}

	
	/*****
	 * 通过其刊名id获取最近的8个杂志
	 */
	public List<MagazineName> loadLastMagazineNameListByIdDao(Integer magazineNameId) throws DataAccessException 
	{
		//先获取当前期的杂志时间
		MagazineName curMagazineName =this.loadById(magazineNameId);
		String createTime = (curMagazineName.getInputtime() )!=null? curMagazineName.getInputtime().toString(): (new Date()).toString();
		
		
		String hql=" from MagazineName as m where m.inputtime < ?  order by m.inputtime desc";
		Query query =this.getSession().createQuery(hql);
		query.setParameter(0, createTime);
		query.setFirstResult(0);
		query.setMaxResults(4);
		
		return query.list();
		
	}

	/*****
	 * 获取最新的一个
	 */
	public Integer loadMagazineLastIdDao() throws DataAccessException 
	{
		Integer lastId=null;
		String hql="from MagazineName as m  order by m.inputtime desc";
		Query query =this.getSession().createQuery(hql);
		query.setFirstResult(0);
		query.setMaxResults(1);
		List li = query.list();
		if(li!=null)
		{
			MagazineName m = (MagazineName)li.get(0);
			lastId = m.getId();
		}
		return lastId;
		
	}

	public List getMagazineClassListByParentId(Integer magazineNameid) throws DataAccessException {
		// TODO Auto-generated method stub
		String sql=" from MagazineClass as m where 1=1 and m.magazineName.id = ? ";
		List list=this.getHibernateTemplate().find(sql, magazineNameid);
		return list;
	}

	
}










